package cn.org.timespace.ons.dao.base.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.base.MachinesConfModel;
import cn.org.timespace.ons.action.base.MachinesModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.base.MachinesConfDao;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.MachinesConf;

public class MachinesConfDaoImpl extends TSBaseDAO implements MachinesConfDao {

	@Override
	public void delete(MachinesConf persistentInstance) {
		// TODO Auto-generated method stub

		log.debug("deleting MachinesConf Instance");
		try{
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public List<MachinesConf> findConfByMachinesID(String  machinesGroupID) {
		log.debug("findByCondition Mom Instance");
		StringBuffer queryString = new StringBuffer("select mc from MachinesConf mc,Machines m where mc.mid = m.mid ");
//		if(!StringUtils.isEmpty(model.getMid())){
//			queryString.append("and m.mid = '" + model.getMid() + "'" );
//		}
		//组合ID
		if(!StringUtils.isEmpty(machinesGroupID)){
			queryString.append("and m.machinesID = '" + machinesGroupID + "'" );
		}
		
		/*
		//厂家随机码
		if(!StringUtils.isEmpty(model.getCompRandom())){
			queryString.append("and m.compRandom = '" + model.getCompRandom() + "'" );
		}
		//机器码
		if(!StringUtils.isEmpty(model.getDeviceCode())){
			queryString.append("and m.deviceCode = '" + model.getDeviceCode() + "'" );
		}
		//机器随机码
		if(!StringUtils.isEmpty(model.getDeviceRandom())){
			queryString.append("and m.deviceRandom = '" + model.getDeviceRandom() + "'" );
		}
		//机器类型
		if(!StringUtils.isEmpty(model.getType())){
			queryString.append("and m.type = '" + model.getType() + "'" );
		}
		//机器状态
		if(!StringUtils.isEmpty(model.getStatus()+"")){
			queryString.append("and m.status = '" + model.getStatus() + "'" );
		}
		//机器名称
		if(!StringUtils.isEmpty(model.getName())){
			queryString.append("and m.name = '" + model.getName() + "'" );
		}
		//机器名称
		if(!StringUtils.isEmpty(model.getAddress())){
			queryString.append("and m.address = '" + model.getAddress() + "'" );
		}
		*/
		queryString.append(" order by m.deviceCode desc");
		return getHibernateTemplate().find(queryString.toString());
	}

	@Override
	public List<MachinesConf> findByCondition(MachinesConfModel model) {
		log.debug("findByCondition Mom Instance");
		StringBuffer queryString = new StringBuffer("from MachinesConf m where 1=1");
		if(!StringUtils.isEmpty(model.getMid())){
			queryString.append("and m.mid = '" + model.getMid() + "'" );
		}
		if(!StringUtils.isEmpty(model.getConfName())){
			queryString.append("and m.confName = '" + model.getConfName() + "'" );
		}
		queryString.append(" order by m.confName desc");
		return getHibernateTemplate().find(queryString.toString());
	}

	@Override
	public List<Object> findByCondition(MachinesConfModel model, Pager p) {
		log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from MachinesConf m where 1=1");
			if(!StringUtils.isEmpty(model.getMid())){
				queryString.append("and m.mid = '" + model.getMid() + "'" );
			}
			if(!StringUtils.isEmpty(model.getConfName())){
				queryString.append("and m.confName = '" + model.getConfName() + "'" );
			}
			queryString.append(" order by m.confName desc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}

	@Override
	public MachinesConf findById(String id) {
		// TODO Auto-generated method stub
		log.debug("getting MachinesConf instance with id: " + id);
		try {
			MachinesConf instance = (MachinesConf) getHibernateTemplate().get(
					"cn.org.timespace.ons.vo.base.MachinesConf", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public void save(MachinesConf transientInstance) {
		// TODO Auto-generated method stub

		log.debug("saving AppleServer instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void update(MachinesConf instance) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(instance);
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	@Override
	public List<MachinesConf> findByCondition(String id) {
		// TODO Auto-generated method stub
		Session session = null;
		Query query = null;
		List<MachinesConf> list = new ArrayList();
		
		try{
			StringBuffer queryString = new StringBuffer("from MachinesConf m where m.mid ='");
				queryString.append(id);
				queryString.append("'");
				
				
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			
			//MachinesConf machinesConf=null;
			 list=query.list();
		
			
		}catch(RuntimeException re){
			re.printStackTrace();
		}
		return list;
	}

}
